package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.dao.SkuDAO;
import com.junzhiit.nengji.shop.model.Sku;

@Repository
public class SkuDAOImpl extends AbstractBaseDAO<Sku> implements SkuDAO {

	@Override
	public List<Sku> findAll(Sku sku, int pageNumber, int maxPageRow) {

		StringBuffer sql = new StringBuffer();
		sql.append(" select * from sku where is_delete=? and shop_id=? ");

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(sku.getShopId());

		if (StringUtils.isNotBlank(sku.getName())) {
			sql.append(" and name like ? ");
			param.setString("%" + sku.getName() + "%");
		}
		sql.append(" order by created_time desc ");

		return super.queryForPagingList(sql.toString(), Sku.class, param, pageNumber, maxPageRow);

	}

	@Override
	public int getTotalCount(Sku sku) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from sku where is_delete=? and shop_id=?");

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(sku.getShopId());

		if (StringUtils.isNotEmpty(sku.getName())) {
			sql.append(" and name like ? ");
			param.setString("%" + sku.getName() + "%");
		}

		return super.queryForInt(sql.toString(), param);
	}

	@Override
	public int queryNameIsExit(String shopId, String name) {
		String sql = "select count(id) from sku where is_delete=? and shop_id=? and name=?";

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(shopId);
		param.setString(name);

		return super.queryForInt(sql, param);
	}
	
	
	@Override
	public int queryGoodsAboutSkuCount(String skuId){
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from goods_sku_items");
		sql.append(" where sku_id=?");
		
		StatementParameter param=new StatementParameter();
		param.setString(skuId);
		
		return super.queryForInt(sql.toString(),param);
		
	}

	@Override
	public List<Sku> queryAllSku(String shopId) {
		String sql = "select id,name from sku where is_delete=? and shop_id=? order by created_time desc";

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(shopId);

		return super.queryForList(sql, Sku.class, param);
	}
	
	@Override
	public Sku findSkuByName(String shopId,String name){
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from sku");
		sql.append(" where shop_id=?");
		sql.append(" and name=? ");
		sql.append(" and is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setString(shopId);
		param.setString(name);
		param.setInt(IsDelete.NO.toIntValue());
		
		return super.query(sql.toString(),Sku.class, param);
	}
	
}
